<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
    <script type="text/javascript">
        var HOST = "http://localhost:8080/KiWi2/";
        var i = 0;
        var points = ["",".","..","..."]
        function checkIsAlive(tname) {
            $.getJSON(HOST+"sn/importer/isActive?tname="+encodeURIComponent(tname),function(data){
                if(data) {
                    $("#loading").text("Importing files. May take some time "+points[i%4]);i++;
                    setTimeout("checkIsAlive('"+tname+"')",8000);
                } else {
                    $("#loading").text("");
                    alert("finished import");
                }
            });
        }

        function doUrlRequest() {
            var xhr = new XMLHttpRequest;
            var url = $("#url").val();
            xhr.open("POST", HOST+"sn/importer/article/application/sn+xml?url="+encodeURIComponent(url), true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if(xhr.status == 200) {
                        $("#loading").text("Importing files. May take some time ...");
                        var result = eval("("+xhr.responseText+")");
                        checkIsAlive(result.tname);
                    } else {
                        alert("Error: (Status "+xhr.status+") "+xhr.responseText);
                    }

                }
            };
            xhr.send(null);
        }

        function doFileRequest() {
            alert("Not implemented yet. Use url importer instead!");
        }
    </script>
</head>
<body>
<h2>SN File Importer</h2>
<!--
<fieldset>
    <legend>Upload file in sn+xml format</legend>
    <table>
        <tr>
            <td>URL:</td>
            <td><input type="file" size="80"></td>
            <td><button onclick="doFileRequest();">import</button></td>
        </tr>
    </table>
</fieldset>
-->
<fieldset>
    <legend>Upload files in sn+xml format from local directory</legend>
    <table>
        <tr>
            <td>URL:</td>
            <td><input type="text" size="80" id="url"></td>
            <td><button onclick="doUrlRequest();">import</button></td>
        </tr>
    </table>
</fieldset>
<b style="color:red" id="loading"></b>
</body>
</html>